home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / cxref.c < prev    next >
C/C++ Source or Header  |  1997-12-16  |  23KB  |  815 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/cxref.c 1.35 1997/11/20 19:57:39 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4a.
  5.   ******************/ /******************
  6.   Written by Andrew M. Bishop
  7.  
  8.   This file Copyright 1995,96,97 Andrew M. Bishop
  9.   It may be distributed under the GNU Public License, version 2, or
  10.   any higher version.  See section COPYING of the GNU Public license
  11.   for conditions under which this file may be redistributed.
  12.   ***************************************/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #ifndef __SASC    /* olsen: does not exist with SAS/C */
  19. #include <sys/wait.h>
  20. #endif /* __SASC */
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23.  
  24. #ifdef AMIGA    /* olsen */
  25. #include "amiga.h"
  26. #endif /* AMIGA */
  27.  
  28. #include "parse-yy.h"
  29. #include "memory.h"
  30. #include "datatype.h"
  31. #include "cxref.h"
  32.  
  33. /*+ The default value of the CPP command. +*/
  34. #ifdef CXREF_CPP
  35. #define CPP_COMMAND CXREF_CPP
  36. #else
  37. #define CPP_COMMAND "gcc -E -C -dD -dI"
  38. #endif
  39.  
  40. /*+ The name of the file to read the configuration from. +*/
  41. #define CXREF_CONFIG_FILE ".cxref"
  42.  
  43.  
  44. static void Usage(int verbose);
  45. static int ParseConfigFile(void);
  46. static int ParseOptions(int nargs,char **args,int is_argv);
  47.  
  48. static int DocumentTheFile(char* name);
  49. static FILE* popen_execvp(char** command);
  50. static int pclose_execvp(FILE* f);
  51.  
  52. static char** cpp_command;              /*+ The actual cpp command that is built up, adding -D, -U and -I options. +*/
  53. static int cpp_command_num=0;           /*+ The number of arguments to the cpp command. +*/
  54.  
  55. /*+ The command line switch that sets the format of the output, +*/
  56. int option_all_comments=0,              /*+ use all comments. +*/
  57.     option_verbatim_comments=0,         /*+ insert the comments verbatim into the output. +*/
  58.     option_block_comments=0,            /*+ remove the leading block comment marker. +*/
  59.     option_no_comments=0,               /*+ ignore all comments. +*/
  60.     option_xref=0,                      /*+ do cross referencing. +*/
  61.     option_warn=0,                      /*+ produce warnings. +*/
  62.     option_index=0,                     /*+ produce an index. +*/
  63.     option_raw=0,                       /*+ produce raw output. +*/
  64.     option_latex=0,                     /*+ produce latex output. +*/
  65.     option_html=0;                      /*+ produce html output. +*/
  66.  
  67. /*+ The option to control the mode of operation. +*/
  68. int option_delete=0;
  69.  
  70. /*+ The command line switch for the output name, +*/
  71. char *option_odir=".",                  /*+ the directory to use. +*/
  72.      *option_name="cxref",              /*+ the base part of the name. +*/
  73.      *option_root=NULL;                 /*+ the source tree root directory. +*/
  74.  
  75. /*+ The name of the include directories specified on the command line. +*/
  76. char **option_incdirs=NULL;
  77.  
  78. /*+ The number of include directories on the command line. +*/
  79. int option_nincdirs=0;
  80.  
  81. /*+ The current file that is being processed. +*/
  82. File CurFile=NULL;
  83.  
  84.  
  85. /*++++++++++++++++++++++++++++++++++++++
  86.   The main function that calls the parser.
  87.  
  88.   int main Returns the status, zero for normal termination, else an error.
  89.  
  90.   int argc The command line number of arguments.
  91.  
  92.   char** argv The actual command line arguments
  93.   ++++++++++++++++++++++++++++++++++++++*/
  94.  
  95. int main(int argc,char** argv)
  96. {
  97.  int i;
  98.  char *root_prefix=NULL;
  99.  
  100.  #ifdef AMIGA /* olsen */
  101.  {
  102.    expand_args(argc,argv,&argc,&argv,0,1);
  103.  }
  104.  #endif /* AMIGA */
  105.  
  106.  if(argc==1)
  107.     Usage(1);
  108.  
  109.  cpp_command=(char**)Malloc(8*sizeof(char*));
  110.  cpp_command[cpp_command_num++]=MallocString(CPP_COMMAND);
  111.  
  112.  for(i=1;i<strlen(CPP_COMMAND);i++)
  113.     if(cpp_command[0][i]==' ')
  114.        cpp_command[0][i]=0;
  115.     else
  116.        if(cpp_command[0][i-1]==0)
  117.          {
  118.           if((cpp_command_num%8)==6)
  119.              cpp_command=(char**)Realloc(cpp_command,(cpp_command_num+10)*sizeof(char*));
  120.           cpp_command[cpp_command_num++]=&cpp_command[0][i];
  121.          }
  122.  
  123.  option_incdirs=(char**)Malloc(8*sizeof(char*));
  124.  option_incdirs[0]=".";
  125.  option_nincdirs=1;
  126.  
  127.  /* Parse the command line options. */
  128.  
  129.  if(ParseOptions(argc-1,&argv[1],1))
  130.     Usage(0);
  131.  
  132.  /* Parse the options in .cxref in this directory. */
  133.  
  134.  if(ParseConfigFile())
  135.     Usage(0);
  136.  
  137.  /* Change directory. */
  138.  
  139.  if(option_root)
  140.    {
  141.     char *here=(char*)Malloc(512),*there=(char*)Malloc(512);
  142.  
  143.     if(!getcwd(there,255))
  144.       {fprintf(stderr,"cxref: Error cannot get current working directory (1).\n");exit(1);}
  145.     if(chdir(option_root))
  146.       {fprintf(stderr,"cxref: Error cannot change directory to '%s'.\n",option_root);exit(1);}
  147.     if(!getcwd(here,255))
  148.       {fprintf(stderr,"cxref: Error cannot get current working directory (2).\n");exit(1);}
  149.  
  150.     if(!strncmp(here,there,strlen(here)))
  151.        root_prefix=there+strlen(here)+1;
  152.     else
  153.       {fprintf(stderr,"cxref: Error the -R option has not specified a parent directory of the current one.\n");exit(1);}
  154.  
  155.     for(i=1;i<cpp_command_num;i++)
  156.        if(cpp_command[i][0]=='-' && cpp_command[i][1]=='I')
  157.          {
  158.           if(cpp_command[i][2]==0)
  159.             {
  160.              if(cpp_command[++i][0]!='/')
  161.                 cpp_command[i]=MallocString(CanonicaliseName(ConcatStrings(3,root_prefix,"/",cpp_command[i])));
  162.             }
  163.           else if(cpp_command[i][2]!='/')
  164.              cpp_command[i]=MallocString(ConcatStrings(2,"-I",CanonicaliseName(ConcatStrings(3,root_prefix,"/",cpp_command[i]+2))));
  165.          }
  166.  
  167.     for(i=0;i<option_nincdirs;i++)
  168.        if(*option_incdirs[i]!='/')
  169.           option_incdirs[i]=MallocString(CanonicaliseName(ConcatStrings(3,root_prefix,"/",option_incdirs[i])));
  170.  
  171.     if(ParseConfigFile())
  172.        Usage(0);
  173.    }
  174.  
  175.  /* Process each file. */
  176.  
  177.  for(i=1;i<argc;i++)
  178.     if(argv[i])
  179.       {
  180.        char *filename=CanonicaliseName(root_prefix?ConcatStrings(3,root_prefix,"/",argv[i]):argv[i]);
  181.  
  182.        if(!option_delete)
  183.          {
  184.           CurFile=NewFile(filename);
  185.  
  186.           if(!DocumentTheFile(filename))
  187.             {
  188.              if(option_xref)
  189.                 CrossReference(CurFile);
  190.  
  191.              if(option_raw || option_warn)
  192.                 WriteWarnRawFile(CurFile);
  193.              if(option_latex)
  194.                 WriteLatexFile(CurFile);
  195.              if(option_html)
  196.                 WriteHTMLFile(CurFile);
  197.             }
  198.  
  199.           ResetLexer();
  200.           ResetParser();
  201.           ResetPreProcAnalyser();
  202.           ResetTypeAnalyser();
  203.           ResetVariableAnalyser();
  204.           ResetFunctionAnalyser();
  205.  
  206.           DeleteFile(CurFile);
  207.           CurFile=NULL;
  208.          }
  209.        else
  210.          {
  211.           CrossReferenceDelete(filename);
  212.  
  213.           WriteLatexFileDelete(filename);
  214.           WriteHTMLFileDelete(filename);
  215.          }
  216.  
  217.        TidyMemory();
  218.       }
  219.  
  220.  /* Create the index */
  221.  
  222.  if(option_index)
  223.    {
  224.     StringList files;
  225.     StringList2 funcs,vars,types;
  226.  
  227.     files=NewStringList();
  228.     funcs=NewStringList2();
  229.     vars=NewStringList2();
  230.     types=NewStringList2();
  231.  
  232.     CreateAppendix(files,funcs,vars,types);
  233.  
  234.     if(option_raw||option_warn)
  235.        WriteWarnRawAppendix(files,funcs,vars,types);
  236.     if(option_latex)
  237.        WriteLatexAppendix(files,funcs,vars,types);
  238.     if(option_html)
  239.        WriteHTMLAppendix(files,funcs,vars,types);
  240.  
  241.     DeleteStringList(files);
  242.     DeleteStringList2(funcs);
  243.     DeleteStringList2(vars);
  244.     DeleteStringList2(types);
  245.  
  246.     TidyMemory();
  247.    }
  248.  
  249.  /* Tidy up */
  250.  
  251.  Free(cpp_command[0]);
  252.  Free(cpp_command);
  253.  
  254.  Free(option_incdirs);
  255.  
  256.  PrintMemoryStatistics();
  257.  
  258.  return(0);
  259. }
  260.  
  261.  
  262. /*++++++++++++++++++++++++++++++++++++++
  263.   Print out the usage instructions.
  264.  
  265.   int verbose If true then output a long version of the information.
  266.   ++++++++++++++++++++++++++++++++++++++*/
  267.  
  268. static void Usage(int verbose)
  269. {
  270.  fputs("\n"
  271.        "              C Cross Referencing & Documenting tool - Version 1.4a\n"
  272.        "              -----------------------------------------------------\n"
  273.        "\n"
  274.        "(c) Andrew M. Bishop 1995,96,97  [       amb@gedanken.demon.co.uk ]\n"
  275.        "                                 [http://www.gedanken.demon.co.uk/]\n"
  276.        "\n"
  277.        "Usage: cxref filename [ ... filename]\n"
  278.        "             [-Odirname] [-Nbasename] [-Rdirname]\n"
  279.        "